﻿using System;
using System.Windows.Forms;
using l1cs_cl.client;
using l1cs_cl.client.receivepackets;
using l1cs_cl.client.transferpackets;

namespace l1cs_cl
{
    public partial class frmSelectCharacter : Form
    {
        private static frmSelectCharacter _Instance;
        public static frmSelectCharacter Instance
        {
            get
            {
                if ( _Instance == null || _Instance.IsDisposed )
                    _Instance = new frmSelectCharacter();
                return _Instance;
            }
        }

        public frmSelectCharacter()
        {
            InitializeComponent();
        }

        // 画面を開く
        private void frmSelectCharacter_Load( object sender, EventArgs e )
        {
            R_Charamount pCharamount = new R_Charamount( PlayerThread.readPacketNoTry() );
            Data.Instance.nowCount = pCharamount.nowcount;
            Data.Instance.maxCount = pCharamount.maxcount;
            for ( int i = 0; i < pCharamount.maxcount; i++ )
            {
                if ( i < pCharamount.nowcount )
                {
                    R_Charlist pCharlist = new R_Charlist( PlayerThread.readPacketNoTry() );
                    Data.Instance.addCharacter( pCharlist.chardata );
                }
                setCharacter( i );
            }
        }

        // 新規キャラクタ作成画面を開く
        private void btnNewChar_Click( object sender, EventArgs e )
        {
            int befNowCount = Data.Instance.nowCount;
            frmNewCharacter.getInstance.StartPosition = FormStartPosition.CenterParent;
            DialogResult result = frmNewCharacter.getInstance.ShowDialog();
            if ( result == DialogResult.OK )
            {
                R_NewCharPack pNewCharPack = new R_NewCharPack( PlayerThread.readPacketNoTry() );
                Data.Instance.addCharacter( pNewCharPack.chardata );
                Data.Instance.nowCount++;
                setCharacter( befNowCount );
            }
        }

        // フォームにキャラクター情報を表示する
        public void setCharacter( int i )
        {
            Button btnLogin = (Button)FindControlByFieldName( this, "btnLogin" + i );
            Label lblName = (Label)FindControlByFieldName( this, "lblName" + i );
            Label lblClan = (Label)FindControlByFieldName( this, "lblClan" + i );
            Label lblLawful = (Label)FindControlByFieldName( this, "lblLawful" + i );
            Label lblClass = (Label)FindControlByFieldName( this, "lblClass" + i );
            Label lblSex = (Label)FindControlByFieldName( this, "lblSex" + i );
            Label lblHp = (Label)FindControlByFieldName( this, "lblHp" + i );
            Label lblMp = (Label)FindControlByFieldName( this, "lblMp" + i );
            Label lblLev = (Label)FindControlByFieldName( this, "lblLev" + i );
            Label lblAc = (Label)FindControlByFieldName( this, "lblAc" + i );
            Label lblStr = (Label)FindControlByFieldName( this, "lblStr" + i );
            Label lblDex = (Label)FindControlByFieldName( this, "lblDex" + i );
            Label lblCon = (Label)FindControlByFieldName( this, "lblCon" + i );
            Label lblWis = (Label)FindControlByFieldName( this, "lblWis" + i );
            Label lblCha = (Label)FindControlByFieldName( this, "lblCha" + i );
            Label lblInt = (Label)FindControlByFieldName( this, "lblInt" + i );
            btnLogin.Enabled = false;
            lblName.Text = "";
            lblClan.Text = "";
            lblLawful.Text = "";
            lblClass.Text = "";
            lblSex.Text = "";
            lblHp.Text = "";
            lblMp.Text = "";
            lblLev.Text = "";
            lblAc.Text = "";
            lblStr.Text = "";
            lblDex.Text = "";
            lblCon.Text = "";
            lblWis.Text = "";
            lblCha.Text = "";
            lblInt.Text = "";

            Character chardata = Data.Instance.getCharacter( i );
            if ( chardata != null )
            {
                btnLogin.Enabled = true;
                lblName.Text = chardata.name;
                lblClan.Text = chardata.clanName;
                lblClass.Text = chardata.getStringClass();
                lblSex.Text = chardata.getStringSex();
                lblLawful.Text = Convert.ToString( chardata.lawful );
                lblHp.Text = Convert.ToString( chardata.hp );
                lblMp.Text = Convert.ToString( chardata.mp );
                lblAc.Text = Convert.ToString( chardata.ac );
                lblLev.Text = Convert.ToString( chardata.lv );
                lblStr.Text = Convert.ToString( chardata.str );
                lblDex.Text = Convert.ToString( chardata.dex );
                lblCon.Text = Convert.ToString( chardata.con );
                lblWis.Text = Convert.ToString( chardata.wis );
                lblCha.Text = Convert.ToString( chardata.cha );
                lblInt.Text = Convert.ToString( chardata.intel );
            }
        }

        // 名前からコントロールを見つける
        public static object FindControlByFieldName( Form frm, string name )
        {
            System.Type t = frm.GetType();

            System.Reflection.FieldInfo fi = t.GetField(
                name,
                System.Reflection.BindingFlags.Public |
                System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Instance |
                System.Reflection.BindingFlags.DeclaredOnly );

            if ( fi == null )
                return null;

            return fi.GetValue( frm );
        }

        // ログインボタン
        private void btnLogin_Click( object sender, EventArgs e )
        {
            String btnName = ( (Button)sender ).Name;
            // 何番目か特定
            int num = Convert.ToInt32( btnName.Replace( "btnLogin", "" ) );
            // キャラクターラベルを特定
            Label lblName = (Label)FindControlByFieldName( this, "lblName" + num );
            // ログインパケット送信
            T_LogintoServer tLogintoServer = new T_LogintoServer( lblName.Text );
            PlayerThread.SendPackets( tLogintoServer.getBytes() );
            Close();
        }

        // 削除ボタン
        private void btnDel_Click( object sender, EventArgs e )
        {
            // ボタン名を特定
            String btnName = ( (Button)sender ).Name;
            // 何番目か特定
            int num = Convert.ToInt32( btnName.Replace( "btnDel", "" ) );
            // キャラクターラベルを特定
            Label lblName = (Label)FindControlByFieldName( this, "lblName" + num );
            // 削除パケット送信
            T_DeleteChar tDeleteChar = new T_DeleteChar( lblName.Text );
            PlayerThread.SendPackets( tDeleteChar.getBytes() );

            R_DeleteCharOK pDeleteCharOK = new R_DeleteCharOK( PlayerThread.readPacketNoTry() );
            if ( pDeleteCharOK.result == R_DeleteCharOK.DELETE_CHAR_NOW )
            {
                // データとフォームからキャラを削除
                Data.Instance.delCharacter( num );
                setCharacter( num );
            }
            else
            {
                MessageBox.Show( "削除は7日後になります" );
            }
        }
    }
}
